package com.video;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.VideoView;

import com.android.volley.toolbox.ImageLoader;
import com.app.net.NetHelper;
import com.bumptech.glide.Glide;
import com.example.com.skills_utf8.R;
import com.google.gson.Gson;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import bean.waqu.SearchGuitar;

/**
 * A placeholder fragment containing a simple view.
 */
public class VideoActivity1Fragment extends Fragment {


    Adapter adapter = new Adapter();

    List<SearchGuitar.DatasEntity> listdata = new ArrayList<SearchGuitar.DatasEntity>();




    public VideoActivity1Fragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_video, container, false);

        ListView listview = (ListView)view.findViewById(R.id.listview);
        listview.setAdapter(adapter);


        /*
        listview.setOnScrollListener(new AbsListView.OnScrollListener() {

            int firstVisibleItem = -1;
            int Last_LastVisiablePosition = -1;
            boolean isRunning = false;


            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub


                if(scrollState == SCROLL_STATE_IDLE)
                    isRunning = false;
                else{
                    isRunning = true;
                }

                if( isRunning == false  ){
                   // adapter.Update( Last_LastVisiablePosition );
                }


            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                //System.out.println("onScroll");
                this.firstVisibleItem = firstVisibleItem;

                int LastVisiablePosition = view.getLastVisiblePosition();

                if( LastVisiablePosition > Last_LastVisiablePosition){
                    System.out.println("向上滑动 ");
                }

                if(LastVisiablePosition < Last_LastVisiablePosition){
                    System.out.println("向下滑动");
                }

                Last_LastVisiablePosition = LastVisiablePosition;


				if(isRunning == false){

                    for(int i =0;i<listdata.size(); i++){
                        View v_child = view.getChildAt(firstVisibleItem);
                        if(v_child != null) {
                            VideoView videoview = (VideoView) v_child.findViewById(R.id.vv_content);
                            if (videoview != null) {
                                System.out.println("video stop: "+i);
                                videoview.pause();
                            }
                        }
                    }


					View v_child = view.getChildAt(firstVisibleItem);
					if(v_child != null){
                        VideoView videoview = (VideoView)v_child.findViewById(R.id.vv_content);
                        if(videoview != null){
                            System.out.println("video play: "+firstVisibleItem);
                            videoview.start();
                        }
					}
				}


            }
        });
        */

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                SearchGuitar.DatasEntity entity = (SearchGuitar.DatasEntity) parent.getItemAtPosition(position);

                Intent intent = new  Intent(getActivity(), PlayVideoActivity.class);
                intent.putExtra("path", listdata.get(position).getPreview());
                startActivity(intent);


            }
        });



        requestNetDate();


        return view;

    }


    class Adapter extends BaseAdapter
    {

        @Override
        public int getCount() {
            return listdata.size();
        }

        @Override
        public Object getItem(int position) {
            return listdata.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null){
                convertView = getActivity().getLayoutInflater().inflate(R.layout.layout_video1, null);
            }


            SearchGuitar.DatasEntity entity = listdata.get(position);


            ImageView iv_logo = (ImageView)convertView.findViewById(R.id.iv_logo);
            TextView tv_title = (TextView)convertView.findViewById(R.id.tv_title);


            Glide.with(getActivity()).load(entity.getBigImgUrl()).into(iv_logo);
            tv_title.setText(entity.getTitle());


            return convertView;
        }
    }



    void requestNetDate(){

        new NetHelper(getActivity()){

            @Override
            public String init(ArrayList<BasicNameValuePair> arg0) {
                // TODO Auto-generated method stub

                return "http://waqu.com/v2.1/m/and/v/general_and/pv.json?ts=1424190570556&partner=0009&appName=guitar&appV=3.3.7&hash=087dd31b744e7298168f1fea705a5c92439a5598&sid=14f65a8f495d&size=5&pos=0&scanned=w4hrm8uq3skw09u8~z8655v76h271qp9r~pmox1t85u70ofejs~1j1fqughjkf4ropc~i93yyduvm77f3lps~";
            }

            @Override
            public void onComplete(String arg0) throws JSONException {
                // TODO Auto-generated method stub


				Gson gson = new Gson();
				SearchGuitar Root = (SearchGuitar)gson.fromJson(arg0, SearchGuitar.class);
                listdata = Root.getDatas();
                adapter.notifyDataSetChanged();



            }

        }.start_GET();

    }



}
